package com.weishi.pay.serviceImpl;

import com.weishi.pay.api.constant.RedisCacheConstants;
import com.weishi.pay.api.dto.VoPaymentLog;
import com.weishi.pay.api.service.PaymentLogService;
import com.weishi.pay.dao.mapper.PaymentLogMapper;
import com.weishi.pay.dao.model.PaymentLog;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
* PaymentLogService实现
* Created by autoCode on 2018/8/15.
* @author autoCode
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class PaymentLogServiceImpl implements PaymentLogService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PaymentLogService.class);

    @Autowired
    PaymentLogMapper paymentLogMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public void insertLog(VoPaymentLog voPaymentLog) {
        //处理重复
        Object payno =  redisTemplate.opsForValue().get(RedisCacheConstants.PAY_REPEAT_INSERT_CODE_PREFIX+voPaymentLog.getPayNo());
        if(payno!=null){
            return;
        }
        PaymentLog paymentLog=new PaymentLog();
        BeanUtils.copyProperties(voPaymentLog,paymentLog);
        paymentLogMapper.insertSelective(paymentLog);
    }

    @Override
    public int selectPayAmount(String payNo) {
        return paymentLogMapper.selectPayAmount(payNo);
    }

    @Override
    public int updatePayLog(VoPaymentLog voPaymentLog) {
        PaymentLog paymentLog=new PaymentLog();
        BeanUtils.copyProperties(voPaymentLog,paymentLog);
        return paymentLogMapper.updatePayLog(paymentLog);
    }

    @Override
    public int selectByMap(Map selectMap) {
        return paymentLogMapper.selectByMap(selectMap);
    }

    @Override
    public String selectOrderNo(String payNo) {
        return paymentLogMapper.selectOrderNo(payNo);

    }

    @Override
    public VoPaymentLog selectOrderDetail(String payNo) {
      PaymentLog paymentLog=  paymentLogMapper.selectOrderDetail(payNo);
      VoPaymentLog voPaymentLog=new VoPaymentLog();
      BeanUtils.copyProperties(paymentLog,voPaymentLog);
        return voPaymentLog;
    }
}